/**************************************************************
 * @file     adc_consume.c
 * @brief    用于处理ADC与DMA搬运的数据
 * @author   Ruilist
 * @date     2025-08-03
 * @version  V1.0.0
 * @copyright Copyright (c) 2025
 *
 * @note     1 tab = 4 space
 *
 * @par 日志:
 * - V1.0.0 2025-07-25 Ruilist
 **************************************************************/

//******************************** Includes *********************************//

#include "adc_consume.h"

//******************************** Includes *********************************//

//******************************** Defines **********************************//

//******************************** Defines **********************************//

//*********************Thread_Func **********************//

osThreadId_t adc_consume_TaskHandle;

const osThreadAttr_t adc_consume_attributes = {
    .name       = "adc_consume_Task",
    .stack_size = 128 * 4,
    .priority   = (osPriority_t)osPriorityNormal,
};

//*********************Thread_Func **********************//

//*********************Queue_Handler ********************//

//*********************Queue_Handler ********************//

//******************************** Declaring ********************************//

/**
 * @brief   adc_consume任务
 *
 * @param   *argument    任务默认参数
 *
 * @return  空
 *
 */
void adc_consume_Task_func( void *argument )
{
    uint32_t buffer_check = 0;
    for ( ;; )
    {
        // log_i( "adc_consume_Task_func start\r\n" )

        // 先查询队列中是否有数据，没有则阻塞
        if ( pdTRUE == xQueuePeek( buffer_completed_queue, &buffer_check, portMAX_DELAY ) )
        {
            // 查询到有数据，先尝试获取互斥锁
            if ( pdTRUE == xSemaphoreTake( xMutex, portMAX_DELAY ) )
            {
                // 拿到互斥锁后再取数据
                if ( pdTRUE == xQueueReceive( buffer_completed_queue, &buffer_check, portMAX_DELAY ) )
                {
                    if ( BUFFER_1 == buffer_check )
                    {
                        log_i( "gp_buffer1 data is [%d]\r\n", ( *gp_buffer1 ) );
                    }
                    else if ( BUFFER_2 == buffer_check )
                    {
                        log_i( "gp_buffer2 data is [%d]\r\n", ( *gp_buffer2 ) );
                    }
                    log_i( "xQueueReceive from buffer_completed_queue data is [%d]\r\n", buffer_check );
                }
                // 数据处理完，释放互斥锁
                if ( pdTRUE != xSemaphoreGive( xMutex ) )
                {
                    log_e( "adc_consume_Task_func xSemaphoreGive is failed\r\n" );
                }
            }
        }
        // osDelay( 1 );
    }
}

//******************************** Declaring ********************************//
